package com.xdjj.market.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdjj.market.common.pojo.ResultDto;
import com.xdjj.market.common.pojo.ResultPageDto;
import com.xdjj.market.pojo.Goods;
import com.xdjj.market.service.GoodsService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

//    @GetMapping("/getGoods")
//    public ResultDto getGoods(){
//        List<Goods> goodsList = goodsService.findGoodsList();
//        //返回列表
//        return ResultDto.success("获取商品列表成功！",goodsList);
//    }

    @PostMapping("/getGoods/{currentPage}/{pageSize}")
    @Parameters({
            @Parameter(name = "currentPage",description = "当前页码",required = true,in = ParameterIn.PATH),
            @Parameter(name = "pageSize",description = "每页最大记录数",required = true,in = ParameterIn.PATH)
    })
    public ResultDto getGoods(@RequestBody Goods searchData,@PathVariable int currentPage,@PathVariable int pageSize){
        Page page = goodsService.findGoodsList(searchData,currentPage,pageSize);
        ResultPageDto resultPageDto = ResultPageDto.of(page.getTotal(),page.getRecords());
        return ResultDto.success("获取商品列表成功！",resultPageDto);
    }

    @PostMapping("/addGoods")
    public ResultDto addGoods(@RequestBody Goods goods){
        goodsService.saveGoods(goods);
        return ResultDto.success("添加商品成功！",null);
    }

    @PostMapping("/updateGoods")
    public ResultDto updateGoods(@RequestBody Goods goods){
        goodsService.updateGoods(goods);
        return ResultDto.success("修改商品信息成功！",null);
    }
    @DeleteMapping("/{id}")
    public ResultDto deleteGoodsById(@PathVariable Integer id){
        goodsService.deleteGoodsById(id);
        return ResultDto.success("删除商品成功！",null);
    }
}
